﻿using System.Linq;
using Complaint.Models.Dtos;

namespace Complaint.Repositories
{
    public class CommentRepository : BaseRepository<Comment>, IRepository<Comment>
    {
        #region Implementation of IRepository<Comment>

        /// <summary>
        /// Creates the passed instance of the type to the data store.
        /// </summary>
        /// <param name="dto">The instance of the type to create to the data store.</param>
        public void Create(Comment dto)
        {
            ObjectContext.AddToComment(dto);
            Update(dto);
        }

        /// <summary>
        /// Retrieves all instances of the type T.
        /// </summary>
        /// <returns>The requested instances.</returns>
        public IQueryable<Comment> FindAll()
        {
            return ObjectContext.Comment;
        }

        /// <summary>
        /// Retrieves an instance of the type T by its given ID from the data store.
        /// </summary>
        /// <param name="id">The ID of the instance to retrieve.</param>
        /// <returns>The requested instance.</returns>
        public Comment FindById(int id)
        {
            return ObjectContext.Comment.FirstOrDefault(c => c.CommentID == id);
        }

        /// <summary>
        /// Retrieves an instance of the type T by its given name from the data store.
        /// </summary>
        /// <param name="name">The name of the instance to retrieve.</param>
        /// <returns>The requested instance.</returns>
        public IQueryable<Comment> FindByName(string name)
        {
            return ObjectContext.Comment.Where(c => c.Name.Contains(name));
        }

        #endregion

        public IQueryable<Comment> FindAll(bool status)
        {
            return ObjectContext.Comment.Where(c => c.Status == status);
        }

        public IQueryable<Comment> FindByComplaintId(int id)
        {
            var comments = FindAll(true).Include("Complaint");
            return comments.Where(c => c.Complaint.ComplaintID == id);
        }
    }
}
